// 获取当前定位



// 根据经纬度数组与当前定位，计算距离最近的经纬度
export const GetDistance=function( lng1,lat1,lng2,lat2 ){
    var radLat1 = lat1*Math.PI / 180.0;
    var radLat2 = lat2*Math.PI / 180.0;
    var latDiff = radLat1 - radLat2;
    var lngDiff = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
    var sub = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(latDiff/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(lngDiff/2),2)));
    sub = sub *6378.137 ;
    sub = Math.round(sub * 10000) / 10000;
    return sub;
}


 // 将图片转为base64
 export const getBase64=function(file) {
    return new Promise(function (resolve, reject) {
      let reader = new FileReader();
      let imgResult = "";
      reader.readAsDataURL(file);
      reader.onload = function () {
        imgResult = reader.result;
      };
      reader.onerror = function (error) {
        reject(error);
      };
      reader.onloadend = function () {
        resolve(imgResult);
      };
    });
  }

   // 地址解析(地址转经纬度)
   export const  getGoogleLngAntLat=function(value) {
    return new Promise(function (resolve, reject) {
      let geocoder = new google.maps.Geocoder();
      geocoder.geocode({ address: value }).then((result) => {
        const { results } = result;
        let address = JSON.parse(JSON.stringify(results, null, 2))[0].geometry
          .location;
        resolve(address);
      });
    });
  }
  // 逆地址解析(经纬度转地址)
  export const  getGoogleAddress = function(value) {
    console.log(value)
    return new Promise(function (resolve, reject) {
      let geocoder = new google.maps.Geocoder();
      geocoder.geocode({ location: value }).then((result) => {
        const { results } = result;
        console.log(result)
        let address=results[0].formatted_address
        resolve(address);
      });
    });
  }
  // export const  getGoogleAddress = function(value) {
  //   console.log(value)
  //   return new Promise(function (resolve, reject) {
  // //     let geocoder = new google.maps.Geocoder();
  // //     geocoder.geocode({ placeId: 'ChIJRWBXmdbF7zYRFIMAFUBVqTI' }).then((result) => {
  // //  console.log("获取地址id")
  // //       console.log(result)
  // //       let address=results[0].formatted_address
  //       resolve('ChIJRWBXmdbF7zYRFIMAFUBVqTI');
  //     // });
  //   });
  // }

  export const getzw = function(name) {
    return new Promise((resolve, reject) => {
      const input = document.getElementById(name);
      const options = {
        country: ["us", "pr", "vi", "gu", "mp"],
        fields: ["address_components", "geometry", "icon", "name"],
        strictBounds: false,
        types: ["establishment"],
      };
      const autocomplete = new google.maps.places.Autocomplete(
        input,
        options
      );
      autocomplete.addListener("place_changed", () => {
        const place = autocomplete.getPlace();
        let address = JSON.parse(
          JSON.stringify(place.geometry, null, 2)
        ).location;
        resolve(address);
      });
    });
  }

  //刷新设置
  export const getRefreshTime=function(value){
    // let refreshTime
    // switch (value) {
    //     case "1":
    //       refreshTime = 60000;
    //       break;
    //     case "2":
    //       refreshTime = 30000;
    //       break;
    //     case "3":
    //       refreshTime = 20000;
    //       break;
    //     case "4":
    //       refreshTime = 10000;
    //       break;
    //     case "5":
    //       refreshTime = 99999999999999999999;
    //       break;
    //   }
      // return refreshTime
      return 5000;
  }